package com.quanyan.stadium.biz.redis.impl;

import com.quanyan.place.entity.apiresp.RespCity;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.stadium.biz.redis.UserCenterCacheDao;
import com.quanyan.stadium.component.cache.MyRedisTemplate;
import com.quanyan.stadium.component.cache.StadiumRedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by lei.w on 2016/8/3.
 */
@Service
public class UserCenterCacheDaoImpl implements UserCenterCacheDao {

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    private final static int EXPIRE_TIME = 7*24*60*60-10;

    @Override
    public RespCity getRespCityById(Integer cityId) {
        RespCity respCity = null;
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.getCityInfoKey(cityId));
        Object o = myRedisTemplate.get(redisKey);
        if (o != null) {
            respCity = (RespCity)o;
        }
        return respCity;
    }

    @Override
    public void putRespCityById(RespCity respCity) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.getCityInfoKey(respCity.getId()));
        myRedisTemplate.set(redisKey, respCity);
        myRedisTemplate.expire(redisKey, EXPIRE_TIME);
    }
}
